{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import string\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "productpath = \"../../postfit_derivatives/\"\n",
    "tbpath = \"../../fits/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "models_linear = [\"fulllinearmodel_fit_table.csv\",\"reducedlinearmodelNegBinom_fit_table.csv\",\n",
    "          \"reducedlinearmodelq0_fit_table.csv\",\"reducedlinearmodelq0ctime_fit_table.csv\"]\n",
    "models_nonlinear = [\"nonlinearmodelq0ctime_fit_table.csv\",\n",
    "                    \"nonlinearmodel_fit_table.csv\"]\n",
    "\n",
    "models = models_linear + models_nonlinear\n",
    "\n",
    "# model_hash = {}\n",
    "# k = -1\n",
    "# for model in models:\n",
    "#     k += 1\n",
    "#     model_hash[model] = string.ascii_uppercase[k]\n",
    "\n",
    "# df = pd.DataFrame.from_dict(model_hash, orient='index')\n",
    "# df.to_csv('../postfit_derivatives/model_hash.csv', header=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "rois = []\n",
    "for model in models:\n",
    "    df = pd.read_csv(tbpath + model) #get rois in all tables (some may have failed)\n",
    "    rois += list(df.roi.unique())\n",
    "\n",
    "    \n",
    "rois = list(set(rois))\n",
    "\n",
    "#get inferred\n",
    "theta = df.columns[2:] \n",
    "ntheta = len(theta)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "roi_us = np.sort([i for i in rois if i[:2]=='US'])#[::-1]\n",
    "roi_other = np.sort([i for i in rois if i[:2]!='US'])#[::-1]\n",
    "rois = list(roi_us) + list(roi_other)\n",
    "\n",
    "dfwaic = pd.DataFrame(columns=['roi', 'model','linbool', 'waic'])\n",
    "\n",
    "k = 0\n",
    "for roi in rois:\n",
    "#     print(roi)\n",
    "    for model in models:\n",
    "        k += 1\n",
    "        df = pd.read_csv(tbpath + model)\n",
    "        try:\n",
    "            measure = df.loc[(df.roi==roi)&(df['quantile']==0.5),'waic'].values[0]\n",
    "#             dfwaic.loc[k] = [roi,model_hash[model],measure]\n",
    "            dfwaic.loc[k] = [roi,model.split('.csv')[0],model in models_linear,measure]\n",
    "        except:\n",
    "#             dfwaic.loc[k] = [roi,model_hash[model],1e6]\n",
    "            dfwaic.loc[k] = [roi,model.split('.csv')[0],model in models_linear,1e6]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfwaic_sorted = dfwaic.sort_values(['waic'],ascending=True).groupby('roi')\n",
    "productname = \"waicsorted_topmodels_perroi.csv\"\n",
    "dfwaic_sorted.head(1).to_csv(productpath+productname,index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9117647058823529\n"
     ]
    }
   ],
   "source": [
    "a = dfwaic_sorted.head(1)\n",
    "# print(1-a['linbool'].values[:])\n",
    "x = ['linear','nonlinear']\n",
    "y = [a['linbool'].values[:],1-a['linbool'].values[:]]\n",
    "# sns.barplot(y=y)\n",
    "print(sum(a['linbool'].values[:])/len(a['linbool'].values[:]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
